package com.application.module_chat2.widgth;

import android.app.Activity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import com.application.module_chat2.R;
import com.application.mylibrary.utils.DensityUtil;
import com.application.mylibrary.utils.LogUtils;
import com.application.mylibrary.widget.keybord.ChatBottomPanel;
import com.application.mylibrary.widget.keybord.utils.EmojiBean;
import com.application.mylibrary.widget.keybord.utils.EmojiFilter;
import com.application.mylibrary.widget.keybord.utils.EmojiUtil;

import java.io.IOException;
import java.lang.ref.WeakReference;
import sj.keyboard.adpater.EmoticonsAdapter;
import sj.keyboard.adpater.PageSetAdapter;
import sj.keyboard.data.EmoticonPageEntity;
import sj.keyboard.data.EmoticonPageSetEntity;
import sj.keyboard.interfaces.EmoticonClickListener;
import sj.keyboard.interfaces.EmoticonDisplayListener;
import sj.keyboard.interfaces.PageViewInstantiateListener;
import sj.keyboard.utils.EmoticonsKeyboardUtils;
import sj.keyboard.utils.imageloader.ImageBase;
import sj.keyboard.widget.EmoticonPageView;
import sj.keyboard.widget.FuncLayout;


/**
 * Created by lixuce on 2019/8/7.
 */
public class ChatInput implements FuncLayout.OnFuncKeyBoardListener {
    private Activity activity;
    private ChatBottomPanel emoticonPanel;
    // emoticon click
    final EmoticonClickListener emoticonClickListener = new EmoticonClickListener() {
        @Override
        public void onEmoticonClick(Object o, int actionType, boolean isDelBtn) {
            if (isDelBtn) {
                int action = KeyEvent.ACTION_DOWN;
                int code = KeyEvent.KEYCODE_DEL;
                KeyEvent event = new KeyEvent(action, code);
                emoticonPanel.getEtChat().onKeyDown(KeyEvent.KEYCODE_DEL, event);
            } else {
                if (o == null) {
                    return;
                }
                String content = null;
                if (o instanceof EmojiBean) {
                    content = ((EmojiBean) o).emoji;
                }
//                int index = emoticonPanel.getEtChat().getSelectionStart();
//                Editable editable = emoticonPanel.getEtChat().getText();
//                editable.insert(index, content);
                try {
                    int emojiSize = EmoticonsKeyboardUtils.getFontHeight(emoticonPanel.getEtChat());
                    if (emojiSize < 1) {
                        emojiSize = DensityUtil.dp2px(activity,activity.getResources().getInteger(R.integer.emoji_width));
                    }

                    EmojiUtil.getInstance().appendEmojiText(activity, emoticonPanel.getEtChat(),
                            emojiSize, content);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    // emoticon instantiate
    final EmoticonDisplayListener emoticonDisplayListener = new EmoticonDisplayListener() {
        @Override
        public void onBindView(int i, ViewGroup viewGroup, EmoticonsAdapter.ViewHolder viewHolder, Object object, final boolean isDelBtn) {
            final EmojiBean emojiBean = (EmojiBean) object;
            if (emojiBean == null && !isDelBtn) {
                return;
            }

            viewHolder.ly_root.setBackgroundResource(R.drawable.icon_emoji_delete);

            if (isDelBtn) {
                viewHolder.iv_emoticon.setImageResource(R.drawable.icon_del);
            } else {
                viewHolder.iv_emoticon.setImageResource(emojiBean.icon);
            }

            viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    emoticonClickListener.onEmoticonClick(emojiBean, 0, isDelBtn);
                }
            });
        }
    };

    //  page instantiate
    PageViewInstantiateListener pageViewInstantiateListener = new PageViewInstantiateListener<EmoticonPageEntity>() {
        @Override
        public View instantiateItem(ViewGroup viewGroup, int i, EmoticonPageEntity pageEntity) {
            if (pageEntity.getRootView() == null) {
                EmoticonPageView pageView = new EmoticonPageView(viewGroup.getContext());
                pageView.setNumColumns(pageEntity.getRow());
                pageEntity.setRootView(pageView);
                try {
                    EmoticonsAdapter adapter = new EmoticonsAdapter(viewGroup.getContext(), pageEntity, null);
                    // emoticon instantiate
                    adapter.setOnDisPlayListener(emoticonDisplayListener);
                    pageView.getEmoticonsGridView().setAdapter(adapter);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return pageEntity.getRootView();
        }
    };

    public ChatInput(WeakReference<Activity> activity) {
        this.activity = activity.get();
        initView();

    }
    public void reset(){
        if (emoticonPanel!=null) {
            emoticonPanel.reset();
        }

    }
    private void initView() {
        if (activity == null) {
            return;
        }
        LogUtils.i("开始添加emoticon");

        emoticonPanel = activity.findViewById(R.id.chat_ek_bar);

        EmoticonPageSetEntity xhsPageSetEntity
                = new EmoticonPageSetEntity.Builder()
                .setLine(4)
                .setRow(7)
                .setEmoticonList(EmojiUtil.getInstance().getEmojiList())
                .setIPageViewInstantiateItem(pageViewInstantiateListener)
                .setShowDelBtn(EmoticonPageEntity.DelBtnStatus.LAST)
                .setIconUri(ImageBase.Scheme.DRAWABLE.toUri("ic_launcher"))
                .build();
        PageSetAdapter pageSetAdapter = new PageSetAdapter();
        pageSetAdapter.add(xhsPageSetEntity);
        emoticonPanel.setAdapter(pageSetAdapter);
        emoticonPanel.getEtChat().addEmoticonFilter(new EmojiFilter());
        emoticonPanel.getBtnSend().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    String s = emoticonPanel.getEtChat().getText().toString();
                    if (!TextUtils.isEmpty(s)) {
                        listener.input(s);
                        emoticonPanel.getEtChat().setText("");
                    }

                }
            }
        });
        emoticonPanel.addOnFuncKeyBoardListener(this);
    }

    private ChatBottomPanel.BottomInputBackListener listener;

    public void setInputListener(ChatBottomPanel.BottomInputBackListener listener) {
        this.listener = listener;
    }

    @Override
    public void OnFuncPop(int i) {
        if (listener!=null) {
            listener.funOpen();
        }

    }

    @Override
    public void OnFuncClose() {
        if (listener!=null) {
            listener.funClose();
        }
    }
}
